package com.bootx.officialAccount.util;

import com.bootx.officialAccount.pojo.AddMaterialResponse;
import com.bootx.officialAccount.pojo.BatchGetMaterial;
import com.bootx.officialAccount.pojo.GetMaterialCount;
import com.bootx.officialAccount.pojo.UploadImg;
import com.bootx.util.JsonUtils;
import com.bootx.util.WebUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class MaterialUtils {
    public static void get_material(String accessToken,String mediaId){
        String url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+accessToken;
        Map<String,Object> map = new HashMap<>();
        map.put("media_id",mediaId);
        String post = WebUtils.post(url, map);
        System.out.println(post);
    }

    public static GetMaterialCount get_materialcount(String accessToken){
        String url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token="+accessToken;
        Map<String,Object> map = new HashMap<>();
        String post = WebUtils.post(url, map);
        System.out.println(post);
        return JsonUtils.toObject(post, new TypeReference<GetMaterialCount>() {});
    }

    public static BatchGetMaterial batchget_material(String accessToken, String type, Integer offset, Integer count){
        String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="+accessToken;
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("offset",offset);
        map.put("count",count);
        String post = WebUtils.postJson(url, JsonUtils.toJson(map),null);
        System.out.println(post);
        return JsonUtils.toObject(post, new TypeReference<BatchGetMaterial>() {});
    }






    public static AddMaterialResponse addMaterial(String accessToken, File file, String type){
        String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+accessToken+"&type="+type;
        String post = WebUtils.uploadFile(url, file,null,null);
        System.out.println(post);
        return JsonUtils.toObject(post, new TypeReference<AddMaterialResponse>() {});
    }

    public static AddMaterialResponse addMaterial(String accessToken, String imageUrl, String type){
        try {
            if(Strings.CI.equalsAny("image",type)){
                File file = new File("C:\\Users\\black\\Desktop\\wechat_2025-08-09_135348_906.png");
                FileUtils.copyURLToFile(new URL(imageUrl),file);
                String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+accessToken+"&type="+type;
                String post = WebUtils.uploadFile(url, file,type,null);
                System.out.println(post);
                return JsonUtils.toObject(post, new TypeReference<AddMaterialResponse>() {});
            }else if(Strings.CI.equalsAny("video",type)){
                File file = new File("C:\\Users\\black\\Desktop\\wechat_2025-08-09_135348_906.mp4");
                FileUtils.copyURLToFile(new URL(imageUrl),file);
                String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+accessToken+"&type="+type;
                Map<String,String> videoInfo = new HashMap<>();
                videoInfo.put("title", FilenameUtils.getBaseName(file.getName()));
                videoInfo.put("introduction", FilenameUtils.getBaseName(file.getName()));
                String post = WebUtils.uploadFile(url, file,type,JsonUtils.toJson(videoInfo));
                System.out.println(post);
                return JsonUtils.toObject(post, new TypeReference<AddMaterialResponse>() {});
            }
            AddMaterialResponse uploadImg = new AddMaterialResponse();
            uploadImg.setErrCode(1);
            uploadImg.setErrMsg("文件类型不正确");
            return uploadImg;

        }catch (Exception e){
            AddMaterialResponse uploadImg = new AddMaterialResponse();
            uploadImg.setErrCode(1);
            uploadImg.setErrMsg(e.getMessage());
            return uploadImg;
        }
    }

    public static UploadImg del_material(String accessToken, String mediaId){
        String url = "https://api.weixin.qq.com/cgi-bin/media/del_material?access_token="+accessToken;
        Map<String,Object> map = new HashMap<>();
        map.put("mediaId",mediaId);
        String post = WebUtils.postJson(url, JsonUtils.toJson(map),null);
        System.out.println(post);
        return null;
    }



}
